package com.homegoods.shoppingcar.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.flexbox.FlexboxLayoutManager;
import com.homegoods.shoppingcar.item.GoodsItem;
import com.homegoods.shoppingcar.item.ShoppingCarEmptyItem;
import com.homegoods.shoppingcar.item.ShoppingCartItem;
import com.homegoods.shoppingcar.item.ShoppingGroupItem;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.mvp.FooterItem;
import com.kedll.library.util.MyUtils;

/**
 * @author QiQi
 * @ClassName: FlexBoxAdapter
 * @date 2018/9/18
 * @description
 */
public class FlexBoxAdapter extends MultiTypeAdapter {

    private int itemWidth;

    public FlexBoxAdapter(Context context, int itemCount, int padding) {
        itemWidth = (context.getResources().getDisplayMetrics().widthPixels - MyUtils.dp2px(padding)) / itemCount;
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        holder.bindTo(getItem(position));
        View view = holder.itemView;
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        if (lp instanceof FlexboxLayoutManager.LayoutParams) {
            FlexboxLayoutManager.LayoutParams flexboxLp = (FlexboxLayoutManager.LayoutParams) lp;
            IItem iItem = getItem(position);
            if (iItem instanceof ShoppingCarEmptyItem
                    || iItem instanceof ShoppingGroupItem
                    || iItem instanceof GoodsItem
                    || iItem instanceof ShoppingCartItem
                    || iItem instanceof FooterItem) {

                flexboxLp.setWrapBefore(true);
            } else {
                flexboxLp.setMaxWidth(itemWidth);
                flexboxLp.setFlexGrow(1.0f);
            }
        }
    }
}
